package com.sap.health.tracker;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.TableRow.LayoutParams;

import com.google.android.gcm.GCMRegistrar;
import com.sap.health.tracker.json.JSONFunctions;
import com.sap.health.tracker.library.StoreData;
import com.sap.health.tracker.library.Utils;

/*
 * Sets up push notification system.
 */
public class GCMNotifications extends Activity {
	
	private static String KEY_UID = "uid";
    private static String KEY_SESSION = "session";
    private Context mCtx;
    
    /**
     * Not using an intent, run on thread instead and using main app context. 
     * @param ctx
     */   
    public GCMNotifications (Context ctx) {
    	this.mCtx = ctx;
		try{
			// Make sure the device has the proper dependencies.
	 		GCMRegistrar.checkDevice(ctx);
	 		// Make sure the manifest was properly set
	 		GCMRegistrar.checkManifest(ctx);
	 		// get the regId from GCM service.
	 		final String regId = GCMRegistrar.getRegistrationId(ctx);	 		

	 		if (regId.equals("")) {
	 			// no id registered yet, so register one.
	 			GCMRegistrar.register(ctx, Utils.GCMSenderId);
	 		} else {
	 			Log.v("", "Already registered: "+regId);
	 		}	 	    
	 		//open local storage connection	 		 
 	        StoreData store = new StoreData();     
 	        // get user id from local storage
 	        final String userId = (String) store.openType(ctx, String.class,
 	        		KEY_UID);
 	        // get session token from local storage
 	        final String session = (String) store.openType(ctx, String.class,
 	        		KEY_SESSION);
 	        // give the server the regId that is registered.
 	        final JSONFunctions userFunctions = new JSONFunctions();
 	        new Thread(new Runnable() {
 	            @Override
 	            public void run() {
 	            	userFunctions.pushNotifications(userId, session, regId);
 	            }
 	        }).start();
	      	
		 } catch(Exception e){
			 //TODO Handle exception in a non generic way.
			 System.out.println("Notifications failure: " + e);
		 }
    }
    
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	/*
	 * dialog of patients, only shows details of the hospital.
	 */
	private void dialog(final String title, final String value){
		final Dialog dialog = new Dialog(mCtx);
		dialog.setContentView(R.layout.popup);
		dialog.setTitle(title);   
		TableRow valueRow = new TableRow(mCtx);
      	// create text view.
      	TextView valueTx = new TextView(mCtx);
      	valueTx.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));           	 
      	valueTx.setText(value);
      	// add views to the row
      	valueRow.addView(valueTx);  	 
      	valueRow.setPadding(10, 20, 0, 0);     
      	TableLayout dialogTable = (TableLayout) dialog.findViewById(R.id.dialogTable);           
      	// add the row to the table layout
      	dialogTable.addView(valueRow);   	      	
		Button dialogButton = (Button) dialog.findViewById(R.id.dismiss);
		// if button is clicked, close the custom dialog
		dialogButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
		dialog.show();
	}
	 
}